import initialEnvParam from '../utils';
import { options } from '../utils/config';

function storeEnv(value, option) {
	if (!option) {
		option = options.find((option) => option.value === value);
	}
	let envParam = { url: value, label: value, value };
	// 存入本地缓存
	localStorage.envParam = JSON.stringify(option ? option : envParam);
	// 重载页面
	setTimeout(() => location.reload(), 300);
}

// ============================================================

export default {
	name: 'chooseEnv',
	isJiess: true,
	setup() {
		const area = this.area({ hasCard: false, formParam: { initialValues } });
		const initialValues = { env: initialEnvParam.label };
		area
			.add(
				area.Autocomplete({
						$name: 'env',
						$label: '选择环境',
						$span: 24,
						options,
					},
					area.Search({
						$hasFormItem: false,
						$hasCol: false,
						enterButton: '确定切换',
						// 不需要提供第二个参数
						onSearch: (value) => {
							storeEnv(value);
							this.dialog().closeAll();
						},
					}),
				),
			)
			.done();
	},
};